<template>
   <el-card class="trademark">
     <template v-slot:header>
       <el-button type="primary" @click='showAdd' v-if="$hasBP('btn.Trademark.add')">添加</el-button>
     </template>
     <div>
       <el-table 
        v-loading="loading"
        :data="trademarks"
        border
        row-key="id">
      <el-table-column
        type=index
        label="序号"
        width="80"
        align="center">
      </el-table-column>
      <el-table-column
        prop="tmName"
        label="品牌名称"
        align="center"
        >
      </el-table-column>
      <el-table-column
        label="品牌Logo"
         align='center'>
        <template v-slot='scope'>
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.logoUrl"
           ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        v-slot="{row}">
        <el-button type="warning" icon="el-icon-edit" size="small" @click='updateTm(row)' v-if="$hasBP('btn.Trademark.update')">修改</el-button>
        <el-button type="danger" icon="el-icon-delete" size="small" @click="delList(row)"  v-if="$hasBP('btn.Trademark.remove')">删除</el-button>
        <!-- <Hintbutton title='删除' type="danger" icon="el-icon-delete" size="small" @click='delList(row)' v-if="$hasBP('btn.Trademark.remove')"></Hintbutton> -->
      </el-table-column>
    </el-table>
     <el-pagination
      @current-change='handleCurrentPage'
      @size-change='handlePageSize'
      :current-page="currentPage"
      :page-sizes="[3, 6, 9]"
      :page-size="pageSize"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total">
    </el-pagination>
    </div>
    <template>
      <el-dialog :title="form.id?'修改品牌':'添加品牌'" :visible.sync="dialogFormVisible">
          <el-form :model="form" label-width="100px" :rules="rules" ref='tmForm'>
            <el-form-item label="品牌名称" prop='tmName'>
              <el-input v-model="form.tmName" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="品牌LOGO" prop='logoUrl'>
              <el-upload
                class="avatar-uploader"
                action="/dev-api/admin/product/fileUpload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="form.logoUrl" :src="form.logoUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addOrUpdate" :loading='addLoading'>确 定</el-button>
        </div>
      </el-dialog>
    </template>
   </el-card>
</template>

<script>
import { trademark } from '@/api';
export default {
  name: 'TrademarkList',
  data() {
    return {
      trademarks: [],  //当前页的数据
      currentPage:1,
      pageSize:3,
      total:0,
      loading:false,
      dialogFormVisible:false,
      form:{
        tmName:'',
        logoUrl:''
      },
      addLoading:false,
      rules: {
        tmName: [
          { required: true, message: '请输入品牌名称' , trigger: 'blur'},
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        logoUrl:[
          { required: true, message: '请输入品牌LOGO', trigger: 'blur' },
        ],
      }
    }
  },
  methods:{
    delList(row){
       this.$confirm(`确定删除${row.tmName}吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await this.$api.trademark.del(row.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          if(this.trademarks.length===0){
            this.currentPage--
          }
          this.getList()
        },()=>{
          this.$message({
            type: 'info',
            message: '已取消删除'
          }); 
        })
    },
    addOrUpdate(){
      this.$refs.tmForm.validate(async (valid)=>{
        if(valid){
          try{
            this.addLoading=true
            const trademark=this.form
            await this.$api.trademark[trademark.id?'update':'add'](trademark)
            this.dialogFormVisible=false
            this.$message.success('操作成功！')
            if(!trademark.id){
              this.currentPage=1
            }
            this.getList()
          }catch(e){

          }finally{
             this.addLoading=false
          }
        }
      })
    },
    updateTm(row){
      this.form={...row}
      // this.form=row
      this.dialogFormVisible=true
    },
     showAdd(trademarks){
       this.form={
         tmName:'',
         logoUrl:''
       }
      this.dialogFormVisible=true
    },
    async getList(){
        this.loading=true
        const result=await this.$api.trademark.getList({
          page:this.currentPage,
          limit:this.pageSize
        })
         this.loading=false
        const {total,records}=result.data;
       
        this.trademarks=records
        this.total=total
    },
    handleCurrentPage(currentPage){
      this.currentPage=currentPage
      this.getList()
    },
    handlePageSize(pageSize){
      this.pageSize=pageSize
      this.currentPage=1
      this.getList()
    },
    handleAvatarSuccess(res, file) {
      // console.log(res);
        const logoUrl=res.data
        this.form.logoUrl=logoUrl
        this.$refs.tmForm.clearValidate('logoUrl')
    },
    beforeAvatarUpload(file) {
      const isJPGOrPNG = file.type === 'image/jpeg'|| file.type === 'image/png';
      const isLt50KB = file.size / 1024  < 50;

      if (!isJPGOrPNG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
      }
      if (!isLt50KB) {
        this.$message.error('上传头像图片大小不能超过 50KB!');
      }
      return isJPGOrPNG && isLt50KB;
    }  
  },
   async mounted(){
    this.getList()
  },
  watch:{
    dialogFormVisible(value){
      if(value){
        this.$nextTick(()=>{
          this.$refs.tmForm.clearValidate()
        })
      }
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<style lang="scss" scoped>
 .trademark {
    .el-pagination {
      margin-top: 20px;
      text-align: center;
    }
  }
 
</style>
